home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / filarry.exe / FILARRAY.CPP < prev    next >
C/C++ Source or Header  |  1993-04-16  |  3KB  |  123 lines

  1. // filarray.cpp -- class FileArray implementation
  2. // class FileArray serves as "File Array"
  3. // Author     : Jian Hua, 04/01/93
  4. // Compiler   : Borland C++ 3.1
  5. // (c) Jian Hua. Source may be used freely if author is acknowledged.
  6. // Object files may be used freely
  7.  
  8. #include <string.h>
  9. #ifndef __MSGSTR_H
  10. #include "filarray.h"
  11. #endif
  12.  
  13. //
  14. // constructor open file
  15. // if open fail or the file is not expected file, set OK = -1
  16. // so later method call no harmness
  17. // this is kind of fault-tolerace
  18. //
  19. FileArray::FileArray(const char* fname)
  20. {
  21.     is.open(fname, ios::in|ios::binary);
  22.     if( is.bad() ) {
  23. #ifdef TEST
  24.         cout << "open file error";
  25. #endif
  26.         OK = -1;
  27.         return;
  28.     }
  29.     memset(&headInfo, 0x0, sizeof(SDTHEADER));
  30.     is.read((char *)&headInfo, sizeof(SDTHEADER));
  31.     if (headInfo.id != 0xA1)
  32.         OK = -1;
  33.     else {
  34.         for(int i=0; i<5; i++)
  35.             ptrStr[i] = 0;
  36.         count = 0;
  37.         OK = 0;
  38.     }
  39. }
  40.  
  41. //
  42. // destructor close file
  43. //
  44. FileArray::~FileArray()
  45. {
  46.     is.close();
  47.     for(int i=0; i<5; i++) {
  48.         if(ptrStr[i] != 0)
  49.             delete [] ptrStr[i];
  50.     }
  51. }
  52.  
  53. //
  54. // operator overloading at here serves as a "file array"
  55. // to extract one string, index by ndx
  56. // the largest string upto 512 character
  57. // if any error occurs, return a null, so caller should check it
  58. //
  59. //
  60. char* FileArray::operator[](int ndx)
  61. {
  62.     if( OK != 0 )
  63.         return NULL;
  64.  
  65.     if( ndx < 0 || ndx > headInfo.offsetArraySize )
  66.         return NULL;
  67.  
  68.     char retStr[512];
  69.     memset(retStr, 0x0, sizeof(retStr));
  70.     long buf;
  71.     long spos = 0;
  72.     if( ndx == 0 ) {
  73.         spos = sizeof(SDTHEADER);
  74.         is.seekg(spos, ios::beg);
  75.         is.getline(retStr, sizeof(retStr), NULL);
  76.  
  77.         updatePtr(strlen(retStr));
  78.         strcpy(ptrStr[count], retStr);
  79.         count += 1;
  80.         return ptrStr[count-1];
  81.     }
  82.     else
  83.         spos = sizeof(SDTHEADER) + headInfo.strCtxLen + ndx * 4;
  84.  
  85.     is.clear();
  86.     is.seekg(spos, ios::beg);
  87.  
  88.     buf = 0;
  89.     is.read((char *)&buf, sizeof(buf));
  90.  
  91.     spos = sizeof(SDTHEADER) + buf;
  92.     is.seekg( (long)spos, ios::beg );
  93.  
  94.     is.getline(retStr, sizeof(retStr), NULL);
  95.  
  96.     updatePtr(strlen(retStr));
  97.     strcpy(ptrStr[count], retStr);
  98.     count += 1;
  99.     return ptrStr[count-1];
  100. }
  101.  
  102. //
  103. // here this I use a set of recycle buffers
  104. // why I use this?
  105. // take look at the example, such as, a function call may
  106. // lokk like:
  107. // func( strFunc(1), strFunc(2), strFunc(3) );
  108. // here is the prototype of strFunc():
  109. //         char* strFunc(int num);
  110. // so consider what will happen when you call func()
  111. //
  112. void FileArray::updatePtr(int len)
  113. {
  114.     if( count == 4 )
  115.         count = 0;
  116.  
  117.     if( ptrStr[count] != NULL )
  118.         delete[] ptrStr[count];
  119.  
  120.     ptrStr[count] = new char[len+1];
  121.  
  122. }
  123.